import {Component, OnInit} from '@angular/core';
import {AddMesTemplateComponent} from '../add-mes-template.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {MesTemplate} from '../../mes-template.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {isNullOrUndefined} from 'util';
import {MesTypeCondition} from '../../../mes-type/mes-type.type';
import {FroalaEditor} from '../../../../../frame/global/froala-editor';

@Component({
    selector: 'app-main-add-mes-template',
    templateUrl: './main-add-mes-template.component.html',
    styleUrls: ['./main-add-mes-template.component.css']
})
export class MainAddMesTemplateComponent implements OnInit {

    froalaEditorOptions = Object.assign(FroalaEditor.options, {});

    errRes = new ErrRes();

    domain = new MesTemplate();

    extend = {
        domain: {
            default: () => {
                this.domain.fkMesType = null;
            },
            mesType: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '消息类型',
                change: (event) => {
                    console.log(event);
                }
            },
        },
    };

    func = {
        save: {
            req: (event) => {
                Services.mesTemplate.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.mesTemplate.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public addMesTemplate: AddMesTemplateComponent) {
        this.addMesTemplate.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
        //
        Services.mesType.list(new MesTypeCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            console.log(item);
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.mesType.items = [];
                item.page.content.forEach(it => {
                    this.extend.domain.mesType.items.push({id: it.uuid, name: it.serName});
                });
            }
        });
    }


}
